<?php

Yii::import('common.models._base.BaseCourseBatche');

class CourseBatche extends BaseCourseBatche {

    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    public $maxPrice;
    public $minPrice;

    public function rules() {
        return array(
            array('course_venue_id, courses_id, date_from, date_to, price, seats', 'required'),
        );
    }

    public function attributeLabels() {
        return array(
            'id' => Yii::t('app', 'ID'),
            'date_from' => Yii::t('app', 'Start Date'),
            'date_to' => Yii::t('app', 'End Date'),
            'price' => Yii::t('app', 'Price'),
            'seats' => Yii::t('app', 'Seats'),
            'course_venue_id' => null,
            'courses_id' => null,
            'courseVenue' => null,
            'courses' => null,
        );
    }

    public static function findMinMaxPriceByClassbatchForLearn($courseId) {
        $criteria = new CDbCriteria;
        $criteria->select = 'Min(price) as minPrice,Max(price) as maxPrice';
        $criteria->condition = 'courses_id=' . $courseId;
        $courseBatchPrice = CourseBatche::model()->find($criteria);
        if ($courseBatchPrice['minPrice'] != $courseBatchPrice['maxPrice']) {
            return ($courseBatchPrice['minPrice']==0)?'N/A':'Start From  ' . number_format($courseBatchPrice['minPrice']) . ' -  ' . number_format($courseBatchPrice['maxPrice']);
        } else {
            return  ($courseBatchPrice['minPrice']==0)?'N/A':number_format($courseBatchPrice['minPrice']);
        }
    }

    public static function findMinMaxPriceByClassbatchforCategoryPage($courseId) {
        $criteria = new CDbCriteria;
        $criteria->select = 'Min(price) as minPrice,Max(price) as maxPrice';
        $criteria->condition = 'courses_id=' . $courseId;
        $courseBatchPrice = CourseBatche::model()->find($criteria);
        if ($courseBatchPrice['minPrice'] != $courseBatchPrice['maxPrice']) {
            return ($courseBatchPrice['minPrice']==0)?'N/A':number_format($courseBatchPrice['minPrice']);
        } else {
            return ($courseBatchPrice['minPrice']==0)?' N/A':' ' . number_format($courseBatchPrice['minPrice']);
        }
    }

    public static function findMinMaxPriceByCoursebatch($courseId) {
        $criteria = new CDbCriteria;
        $criteria->select = 'Min(price) as minPrice,Max(price) as maxPrice';
        $criteria->condition = 'courses_id=' . $courseId;
        $courseBatchPrice = CourseBatche::model()->find($criteria);
        if ($courseBatchPrice['minPrice'] != $courseBatchPrice['maxPrice']) {
            return ($courseBatchPrice['minPrice']==0)?'N/A':'Start From <span class="rupee">' . number_format($courseBatchPrice['minPrice'], 0) . ' - ' . number_format($courseBatchPrice['maxPrice'], 0).'</span>';
        } else {
            return ($courseBatchPrice['minPrice']==0)?'Price: N/A':'Price <span class="rupee">' . number_format($courseBatchPrice['minPrice'], 0).'</span>';
        }
    }
     public static function seatsAvalableClass($classModel) {
        $seats = '';
        $batchSeats = Yii::app()->db->createCommand()
                ->select('sum(seats) AS seats')
                ->from('skill_course_batche')
                ->where('courses_id=' . $classModel->id)
                ->queryRow();
      
            $querySeats = MakeQuery::model()->findAll('courses_id=' . $classModel->id . ' AND is_converted=' . SkillConfig::STATUS_ACTIVE);
            $seats = count($querySeats);        
        $batchSeats = $batchSeats['seats'] - $seats;      

        return $batchSeats;
    }

    function getId() {
        return $this->id;
    }

    function getPrice() {
        return $this->price;
    }

}
